import ServerUrl from "domainConfig.js";

let uploadFile = function (filePath) {
  let token = wx.getStorageSync("token");
  let promise = new Promise(function (resolve, reject) {
    return wx.uploadFile({
      url: ServerUrl + "/upload",
      filePath: filePath,
      name: "file",
      header: {
        'Authorization': token,
      },
      success(res) {
        let data = JSON.parse(res.data);
        if (data.code == 200) {
          resolve(data);
        } else {
          wx.showModal({
            title: "提示",
            content: data.msg,
            showCancel: false,
          });
          resolve(data);
        }
      },
      fail(error) {
        console.log(error);
        reject(error);
      },
    });
  });
  return promise;
};

export default uploadFile;
